//
//  CMJTabbar.m
//  CMJWeibo
//
//  Created by BH on 15/12/3.
//
//

#import "CMJTabbar.h"
#import "CMJTabbarButton.h"
#import "CMJCommon.h"
#import "UIImage+MJ.h"
#import "UIView+MJ.h"

@interface CMJTabbar ()

@property(nonatomic,weak)UIButton *plusButton;
@property(nonatomic,strong)NSMutableArray *tabbarButtons;
@property(nonatomic,weak)CMJTabbarButton *selectTabbarButton;

@end

@implementation CMJTabbar

-(id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
//        设置背景
        if (!iOS7) {
            self.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageWithName:@"tabbar_background"]];
        }
    }
    return self;
}

/**
 *  数组的懒加载
 */
-(NSMutableArray *)tabbarButtons
{
    if (!_tabbarButtons) {
        self.tabbarButtons = [NSMutableArray array];
    }
    return _tabbarButtons;
}

/**
 *  加号按钮的懒加载
 */
-(UIButton *)plusButton
{
    if (!_plusButton) {
        UIButton *button = [[UIButton alloc] init];
        //设置背景
        [button setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button"] forState:UIControlStateNormal];
        [button setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button_highlighted"] forState:UIControlStateHighlighted];
        
        //设置图标
        [button setImage:[UIImage imageNamed:@"tabbar_compose_icon_add"] forState:UIControlStateNormal];
        [button setImage:[UIImage imageNamed:@"tabbar_compose_icon_add_highlighted"] forState:UIControlStateHighlighted];
        
        //添加事件
        [button addTarget:self action:@selector(plusClick) forControlEvents:UIControlEventTouchUpInside];
        
        [self addSubview:button];
        self.plusButton = button;
    }
    return _plusButton;
}

/**
 *  点击加号按钮
 */
-(void)plusClick
{
    //通知代理
    if ([self.delegate respondsToSelector:@selector(tabbarDidClickPlusButton:)]) {
        [self.delegate tabbarDidClickPlusButton:self];
    }
}

/**
 *  添加一个选项卡按钮
 *
 *  @param item 选项卡按钮对应的模型数据(标题\图标\选中的图标)
 */
-(void)addTabbarItem:(UITabBarItem *)item
{
    // 创建按钮
    CMJTabbarButton *button = [[CMJTabbarButton alloc] init];
    button.item = item;
    button.tag = self.tabbarButtons.count;
    [button addTarget:self action:@selector(tabbarButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:button];
    
    // 加到数组中
    [self.tabbarButtons addObject:button];
    
    //默认选中第一个按钮
    if (self.tabbarButtons.count == 1) {
        [self tabbarButtonClick:button];
    }
}

/**
 *  点击选项卡按钮
 */
-(void)tabbarButtonClick:(CMJTabbarButton *)sender
{
    //通知代理
    if ([self.delegate respondsToSelector:@selector(tabbar:didSelectButtonFrom:to:)]) {
        NSInteger from = self.selectTabbarButton.tag;
        NSInteger to = sender.tag;
        
        [self.delegate tabbar:self didSelectButtonFrom:from to:to];
    }
    NSLog(@"%ld",sender.tag);
    // 更改按钮状态
    self.selectTabbarButton.selected = NO;
    sender.selected = YES;
    self.selectTabbarButton = sender;
}

/**
 *  布局子控件
 */
- (void)layoutSubviews
{
    [super layoutSubviews];
    
    //计算加号按钮的位置和尺寸
    [self setupPlusButtonFrame];
    
    //设置选项卡按钮的位置和尺寸
    [self setupTabBarButtonFrame];
}

/**
 *  设置选项卡按钮的位置和尺寸
 */
-(void)setupTabBarButtonFrame
{
    NSInteger count = self.tabbarButtons.count;
    CGFloat buttonW = self.width/(count+1);
    CGFloat buttonH = self.height;
    
    for (int i=0; i<count; i++) {
        CMJTabbarButton *button = self.tabbarButtons[i];
        button.width = buttonW;
        button.height = buttonH;
        button.x = buttonW*i;
        button.y = 0;
        
        if (i>=2) {
            button.x += buttonW;
        }
    }
}

/**
 *  计算加号按钮的位置和尺寸
 */
- (void)setupPlusButtonFrame
{
    CGSize plusBtnSize = self.plusButton.currentBackgroundImage.size;
    
    //设置尺寸
    self.plusButton.width = plusBtnSize.width;
    self.plusButton.height = plusBtnSize.height;
    //设置中点
    self.plusButton.centerX = self.width*0.5;
    self.plusButton.centerY = self.height*0.5;
}

@end
